package com.ehome.poct.terminal.controller;

import cn.hutool.core.collection.CollUtil;
import com.ehome.poct.terminal.api.constant.DeviceType;
import com.ehome.poct.terminal.api.constant.PackageType;
import com.ehome.poct.terminal.api.constant.VersionStatus;
import com.ehome.poct.terminal.api.dto.TestDeviceVersionDTO;
import com.ehome.poct.terminal.api.dto.TestDeviceVersionRelationDTO;
import com.ehome.poct.terminal.api.dto.VersionReportDTO;
import com.ehome.poct.terminal.api.fegin.TestDeviceFeginClient;
import com.ehome.poct.terminal.api.fegin.TestDeviceVersionFeginClient;
import com.ehome.poct.terminal.api.fegin.TestDeviceVersionRelationFeginClient;
import com.ehome.poct.terminal.api.qry.*;
import com.ehome.poct.terminal.api.vo.FirmwareUpgradeVO;
import com.ehome.poct.terminal.api.vo.NewSoftwareVersionVO;
import com.ehome.poct.terminal.api.vo.NewVersionVO;
import com.ehome.poct.terminal.api.vo.SoftwareUpgradeVO;
import com.ehome.poct.terminal.mapping.UpgradeMapping;
import com.ehome.poct.terminal.service.UpgradeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "检测升级")
@ApiSupport(author = "路飞")
@RestController
@RequestMapping("/test_upgrade")
public class UpgradeController {

    @Autowired
    TestDeviceVersionFeginClient testDeviceVersionFeginClient;
    @Autowired
    TestDeviceFeginClient testDeviceFeginClient;
    @Autowired
    TestDeviceVersionRelationFeginClient testDeviceVersionRelationFeginClient;
    @Autowired
    UpgradeMapping upgradeMapping;
    @Autowired
    UpgradeService upgradeService;

    @ApiOperation("获取下一个驱动版本")
    @GetMapping("/new_driver_version")
    public List<NewSoftwareVersionVO> getNewDriverVersion(NewSoftwareVersionQry qry) {
        return upgradeService.getNewDriverVersion(qry);
    }

    @ApiOperation("获取下一个系统版本")
    @GetMapping("/new_firmware_version")
    public NewVersionVO getNewFirmwareVersion(NewVersionQry qry) {
        return upgradeService.getNewFirmwareVersion(qry);
    }

    @ApiOperation("获取下一个软件版本")
    @GetMapping("/new_software_version")
    public List<NewSoftwareVersionVO> getNewSoftwareVersion(NewSoftwareVersionQry qry) {
        return upgradeService.getNewSoftwareVersion(qry);
    }

    @ApiOperation("获取当前最新的驱动版本")
    @GetMapping("/current_driver_version")
    public List<NewSoftwareVersionVO> getCurrentDriverVersion(CurrentDriverVersionQry qry) {
        qry.setPackageType(PackageType.DRIVER.ordinal());
        qry.setVersionStatus(VersionStatus.PUBLISHED.ordinal());
        qry.setMinSoftwareVersion(qry.getCurrentSoftwareVersion());
        List<NewSoftwareVersionVO> list = testDeviceVersionFeginClient.getNewSoftwareVersion(qry);
        if (list != null) {
            list.forEach(f -> f.setProjectName(qry.getProjectName()));
        }
        return list;
    }

    @ApiOperation("获取当前最新的系统版本")
    @GetMapping("/current_firmware_version")
    public NewVersionVO getCurrentFirmwareVersion(CurrentFirmwareVersionQry qry) {
        qry.setPackageType(PackageType.FIRMWARE.ordinal());
        qry.setVersionStatus(VersionStatus.PUBLISHED.ordinal());
        NewVersionVO newVersion = CollUtil.getFirst(testDeviceVersionFeginClient.getNewFirmwareVersion(qry));
        if (newVersion != null) {
            newVersion.setProjectName(qry.getProjectName());
        }
        return newVersion;
    }

    @ApiOperation("获取当前最新的软件版本")
    @GetMapping("/current_software_version")
    public List<NewSoftwareVersionVO> getCurrentSoftwareVersion(CurrentSoftwareVersionQry qry) {
        qry.setPackageType(PackageType.SOFTWARE.ordinal());
        qry.setMinFirmwareVersion(qry.getCurrentFirmwareVersion());
        qry.setVersionStatus(VersionStatus.PUBLISHED.ordinal());
        List<NewSoftwareVersionVO> list = testDeviceVersionFeginClient.getNewSoftwareVersion(qry);
        if (list != null) {
            list.forEach(f -> f.setProjectName(qry.getProjectName()));
        }
        return list;
    }

    @ApiOperation("驱动升级")
    @ApiOperationSupport(author = "路飞", order = 1)
    @PostMapping("/driver_upgrade")
    public List<SoftwareUpgradeVO> driverUpgrade(@RequestBody SoftwareUpgradeQry qry) {
        qry.setPackageType(PackageType.DRIVER.ordinal());
        List<TestDeviceVersionDTO> list = testDeviceVersionFeginClient.getSoftwareUpgradeInfo(qry);
        return upgradeMapping.toSoftwareVOs(list);
    }

    @ApiOperation("系统升级")
    @ApiOperationSupport(author = "路飞", order = 1)
    @GetMapping("/firmware_upgrade")
    public FirmwareUpgradeVO firmwareUpgrade(FirmwareUpgradeQry qry) {
        qry.setPackageType(PackageType.FIRMWARE.ordinal());
        TestDeviceVersionDTO upgradeInfo = testDeviceVersionFeginClient.getUpgradeInfo(qry);
        return upgradeMapping.toFirmwareVO(upgradeInfo);
    }

    @ApiOperation("软件升级")
    @ApiOperationSupport(author = "路飞", order = 1)
    @PostMapping("/software_upgrade")
    public List<SoftwareUpgradeVO> softwareUpgrade(@RequestBody SoftwareUpgradeQry qry) {
        qry.setPackageType(PackageType.SOFTWARE.ordinal());
        List<TestDeviceVersionDTO> list = testDeviceVersionFeginClient.getSoftwareUpgradeInfo(qry);
        return upgradeMapping.toSoftwareVOs(list);
    }

    @ApiOperation("版本上报")
    @ApiOperationSupport(author = "路飞", order = 1)
    @PostMapping("/version_report")
    public void versionReport(@RequestBody List<VersionReportDTO> list) {
        for (VersionReportDTO dto : list) {
            testDeviceFeginClient.versionReport(dto);
        }
    }

    @ApiOperation("版本初始化")
    @ApiOperationSupport(author = "路飞", order = 1)
    @PostMapping("/version_init")
    public void versionInit(@RequestBody List<TestDeviceVersionRelationDTO> list) {
        for (TestDeviceVersionRelationDTO dto : list) {
            dto.setDeviceType(DeviceType.getDeviceType(dto.getProjectName()));
            testDeviceVersionRelationFeginClient.versionInit(dto);
        }
    }

}
